<template>
  <div class="boxContent">
    <div class="appBox">
      <div class="searchList">
        <el-form :model="state.params"
                 ref="FormInstance "
                 class="boxFrom">
          <div class="searchBox">
            <template v-for="(item, index) in state.totalInputColumns"
                      :key="index">
              <el-form-item v-if="item.type == 'input'">
                <el-input v-model="state.params[item.key]"
                          clearable
                          :placeholder="item.label"></el-input>
              </el-form-item>
              <el-form-item v-if="item.type == 'time'">
                <el-date-picker v-model="state.params[item.key]"
                                type="daterange"
                                value-format="YYYY-MM-DD"
                                start-placeholder="提交开始日期"
                                range-separator="至"
                                end-placeholder="提交结束日期"
                                :placeholder="item.label" />
              </el-form-item>
              <el-form-item v-if="item.type == 'select'">
                <el-select v-model="state.params[item.key]"
                           :placeholder="item.label"
                           clearable
                           v-if="item.key == 'companyId'">
                  <el-option />
                </el-select>

                <el-select v-model="state.params[item.key]"
                           :placeholder="item.label"
                           clearable
                           v-if="item.key == 'approveStatus'">

                  <el-option label="待审核"
                             value="1" />
                  <el-option label="已通过"
                             value="2" />
                  <el-option label="已拒绝"
                             value="3" />

                </el-select>
                <el-select v-model="state.params[item.key]"
                           :placeholder="item.label"
                           clearable
                           v-if="item.key == 'typeId'">
                  <el-option :label="i.dictValue"
                             :value="i.id"
                             v-for="i in state.totalInputColumnsOption.positionType"
                             :key="i" />
                </el-select>
                <el-select style="width: 130px"
                           v-model="state.params[item.key]"
                           :placeholder="item.label"
                           clearable
                           v-if="item.key == 'startYear'">
                  <el-option v-for="i in state.totalInputColumnsOption.startYearList"
                             :key="i"
                             :label="i.label"
                             :value="i.value" />
                </el-select>

              </el-form-item>
            </template>
          </div>

          <el-form-item class="btns">
            <el-button v-preventReClick
                       type="primary"
                       @click="methods.handleAction('export')"
                       v-permission="1192">导出</el-button>
            <el-button v-preventReClick
                       type="primary"
                       plain
                       @click="methods.handleAction('query')">搜索</el-button>
            <el-button v-preventReClick
                       @click="methods.handleAction('reset')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="tabaleBox">
        <el-table border
                  :data="state.tableData"
                  style="width: 100%"
                  :header-cell-style="{ color: '#000' }"
                  v-loading="state.loading">
          <el-table-column prop="cname"
                           label="企业名称"
                           show-overflow-tooltip
                           align="center" />
          <el-table-column prop="dayRange"
                           label="起止时间"
                           show-overflow-tooltip
                           align="center"
                           width="200">

          </el-table-column>
          <el-table-column prop="editDay"
                           label="修改天数"
                           show-overflow-tooltip
                           align="center"
                           width="150" />

          <el-table-column prop="editPerson"
                           label="修改考勤人次	"
                           show-overflow-tooltip
                           width="120"
                           align="center" />

          <el-table-column prop="amount"
                           label="原扣费金额	"
                           show-overflow-tooltip
                           align="center"
                           width="150" />
          <el-table-column prop="editAmount"
                           label="修改后扣费	"
                           show-overflow-tooltip
                           align="center"
                           width="150" />
          <el-table-column prop="updatedAt"
                           label="提交修改时间	"
                           show-overflow-tooltip
                           align="center"
                           width="180" />

          <el-table-column prop="status"
                           label="状态"
                           show-overflow-tooltip
                           align="center"
                           width="150">
            <template #default="scope">

              <div v-if="scope.row.approveStatus == 1"
                   class="warning">待审核</div>
              <div v-if="scope.row.approveStatus == 2"
                   class="primary">已通过</div>
              <div v-if="scope.row.approveStatus == 3"
                   class="error">已拒绝</div>
            </template>
          </el-table-column>

          <el-table-column prop="btns"
                           label="操作"
                           show-overflow-tooltip
                           align="center"
                           width="110"
                           fixed="right">
            <template #default="scope">
              <div class=" btnsDatil">
                <div @click="methods.handleAction('detail', scope.row)"
                     v-permission="1193">
                  查看详情
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination">
        <el-pagination background
                       v-model:current-page="state.params.pageNum"
                       v-model:page-size="state.params.pageSize"
                       :page-sizes="[10, 20, 30, 50]"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="state.totalSize"
                       @size-change="methods.handleSizeChange"
                       @current-change="methods.handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, reactive, getCurrentInstance } from "vue";
import { ElMessage, ElLoading, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
const router = useRouter();
const app = getCurrentInstance()?.proxy;
const initState = () => {
  return {
    loading: false,
    propsCity: [],
    dialogImg: false,
    dialogImgData: null,
    previewImgData: [],
    dialogDescribeData: null,
    dialogDescribe: false,
    totalSize: 0,
    tableData: [],
    params: {
      pageNum: 1,
      pageSize: 10,
      uid: JSON.parse(localStorage.getItem('userInfor')).uid,
    },
    totalInputColumns: [
      { key: "cname", label: "企业名称", type: "input" },
      { key: "date", label: "选择日期 ", type: "time" },
      { key: "approveStatus", label: "状态 ", type: "select" },
    ],
    totalInputColumnsOption: {
      cityList: [],
      position: [],
      positionType: [],
    },
  };
};
const state = reactive(initState());
onMounted(() => {
  methods.getDataList();
});
const methods = {
  handleAction (type, data) {
    switch (type) {
      case "export": //导出
        methods.export();
        break;
      case "detail":
        app.$router.push('/clockln/examine/detail?recordId=' + data.recordId + '&rid=' + data.id)
        break;
      case "query": //查询
        methods.getDataList();
        break;
      case "reset": // 重置
        state.params = initState().params;
        break;
      case "edit": // 编辑
        router.push("/positionAdd?id=" + data.id);
        break;

    }
  },
  tableBtns (data, index) {
    app.$http.post('/sys/companystaff/company/signin/handle', { id: data.id, status: index }).then(res => {
      if (res.code == 200) {
        ElMessage({ type: 'success', message: '操作成功' })
        methods.getDataList()
      }
    })
  },
  // 导出
  export () {
    let data = JSON.parse(JSON.stringify(state.params))
    data.date = data.date ? data.date.join() : ""
    app.$http
      .post(
        `companyCheckAttendanceRecord/exportRecord`, data,
        { responseType: "arraybuffer" }
      )
      .then((res) => {
        if (res) {
          app.$parse.downloadExcel(res, '打卡审核数据')
        }
      });

  },

  // 企业签到|修改|离职接口
  operates (id, status) {
    app.$http
      .post("/sys/companystaff/company/signin/handle", {
        id: id,
        status: status,
      })
      .then((res) => { });
  },
  getDataList () {
    state.loading = true;
    let data = JSON.parse(JSON.stringify(state.params))
    data.date = data.date ? data.date.join() : ""
    app.$get("/companyCheckAttendanceRecord/plat/query", data)
      .then((res) => {
        if (res.data) {
          state.tableData = res.data.data;
          state.totalSize = res.data.total;
        }
        state.loading = false;
      })
      .catch(() => {
        state.loading = false;
      });
  },
  //分页变化
  handleSizeChange () {
    state.params.pageNum = 1;
    methods.getDataList();
  },
  //表格翻页
  handleCurrentChange () {
    methods.getDataList();
  },
};
</script>

<style lang="scss" scoped>
.appBox {
  background: #fff;
  border-radius: 5px;
  padding: 20px;
  height: calc(100% - 20px);
  width: 100%;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  .tabaleBox {
    overflow: hidden;
    flex: 1;
  }
  .btnsDatil {
    color: #0079fe;
    cursor: pointer;
  }
  .pagination {
    margin-top: 20px;
    height: 50px;
    display: flex;
    justify-content: flex-end;
  }
  ::v-deep(.el-dialog__body) {
    max-height: 380px;
  }
  .imgs {
    flex-wrap: wrap;
    .forbox {
      height: 150px;
      margin: 10px 0 0 10px;
    }
    .imgBox {
      height: 100%;
      width: auto;
    }
    .videoBox {
      width: auto;
      height: 100%;
    }
  }
}
</style>
